package com.hunnyhu.tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DetailsActivity extends Activity {

	protected String format = "yyyy-MM-dd HH:mm:ss";
	protected EditText editSignIn;
	protected EditText editSignOut;
	protected TextView hiddenText;
	protected Button updateSignDate;
	protected Button deleteSignDate;
	protected SignDao signDao;
	protected String id = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		id = (null == bundle) ? "" : bundle.getString("id");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);
		hiddenText = (TextView)findViewById(R.id.hiddenId);
		hiddenText.setText(id);
		editSignIn = (EditText) findViewById(R.id.signIn);
		editSignOut = (EditText) findViewById(R.id.signOut);
		updateSignDate = (Button)findViewById(R.id.updateSignDate);
		updateSignDate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("========>Update Id:", hiddenText.getText().toString());
				if (getSignDao().updateCheckDate(Utils.stringToDate(editSignIn.getText().toString(), Utils.YYYY_MM_DD_HH_mm_ss), 
						Utils.stringToDate(editSignOut.getText().toString(), Utils.YYYY_MM_DD_HH_mm_ss), 
						hiddenText.getText().toString()) > 0) {
					makeText("更新成功！");
					finish();
				} else {
					makeText("更新失败！");
				}
			}
		});
		deleteSignDate = (Button)findViewById(R.id.deleteSignDate);
		deleteSignDate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				deleteEvent();
			}
		});
	}
	
	protected void onResume() {
		super.onResume();
		Map<String, Object> object = getSignDao().findById(id);
		if (null == object || object.isEmpty()) {
			Toast.makeText(this, "ID为" + id + "的记录不存在！", Toast.LENGTH_SHORT)
					.show();
			finish();
			return;
		}
		String sDate = (String) object.get(DBHelper.C_DATE_BEGIN);
		sDate = (null == sDate) ? "" : sDate;
		String eDate = (String) object.get(DBHelper.C_DATE_END);
		eDate = (null == eDate) ? "" : eDate;
		editSignIn.setText(sDate);
		editSignIn.setInputType(InputType.TYPE_NULL);//禁止输入法
		editSignIn.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					clickEvent(editSignIn);
				}
				
			}
		});
		editSignIn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (editSignIn.isFocused()) {
					clickEvent(editSignIn);
				}
			}
		});
		editSignOut.setText(eDate);
		editSignOut.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					clickEvent(editSignOut);
				}
				
			}
		});
		editSignOut.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (editSignOut.isFocused()) {
					clickEvent(editSignOut);
				}
			}
		});
		editSignOut.setInputType(InputType.TYPE_NULL);//禁止输入法
	}
	
	protected void clickEvent(EditText editText) {
		String initDate = null;
		if ("".equals(editText.getText().toString())) {
			initDate = format(new Date());
		} else {
			initDate = editText.getText().toString();
		}
		DateTimePickDialog dateTimePicKDialog = new DateTimePickDialog(
				DetailsActivity.this, initDate);
		dateTimePicKDialog.dateTimePicKDialog(editText);
	}
	
	protected void deleteEvent() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("确认");
		dialog.setMessage("确认删除本记录？");
		dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (getSignDao().deleteByPrimaryKey(hiddenText.getText().toString()) > 0) {
					makeText("删除成功！");
					finish();
				} else {
					makeText("删除失败！");
				}
			}
		});
		dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		dialog.show();
	}
	
	protected void makeText(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
	protected String format(Date date) {
		return new SimpleDateFormat(format, Locale.CHINA).format(date);
	}

	public SignDao getSignDao() {
		if (null == signDao) {
			signDao = new SignDao(this);
		}
		return signDao;
	}

}
